package org.example.config;

import lombok.extern.slf4j.Slf4j;
import org.example.commmon.JacksonObjectMapper;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;


@Slf4j
@Configuration


public class WebMvcConfig extends WebMvcConfigurationSupport{


    /**
     对于静态资源的访问 需要添加映射 url中出现的/backend/**都会映射到classpath:/backend/下面
     *
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
        log.info("静态资源映射成功");
    }

    /**
     * 关联到JacksonObjectMapper
     * @param converters
     */
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //创建消息转换器对象
        MappingJackson2HttpMessageConverter messageConverter=new MappingJackson2HttpMessageConverter();
        //设置对象,底层使用Jackson将Java对象转换为json
        messageConverter.setObjectMapper(new JacksonObjectMapper());
        //将对象追加到mvc框架的转换器集合 并设置最高优先级（index=0）
        converters.add(0,messageConverter);
    }
}
